const middleNode = head => {
  if (head == null || head.next == null) return head
  let length = 1, current = head, curr = head
  while (current.next) {
    current = current.next
    length++
  }
  let ans = Math.ceil((length + 1) / 2), i = 1
  while (i < ans && curr.next) {
    curr = curr.next
    i++
  }
  return curr
}

const head = {
  val: 1,
  next: {
    val: 2,
    next: {
      val: 3,
      next: {
        val: 4,
        next: {
          val: 5,
          next: {
            val: 6,
            next: null
          }
        }
      }
    }
  }
}

console.log(middleNode(head));